package com.hyl.config;

import org.apache.commons.dbcp2.BasicDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * MyBatisConfig
 *
 * @author hyl
 * @date 2025-06-27
*/
@Configuration
@PropertySource("classpath:database.properties")
public class MyBatisConfig1 {

    @Value("${mysql.driver}")
    private String driverClassName;

    @Value("${mysql.url}")
    private String url;

    @Bean //返回值=》bean类型 ； 方法名=》bean的id
    public BasicDataSource basicDataSource1() {
        BasicDataSource basicDataSource = new BasicDataSource();
        /*数据库链接的基本配置信息*/
        basicDataSource.setDriverClassName(driverClassName);
        basicDataSource.setUrl(url);
        basicDataSource.setUsername("root");
        basicDataSource.setPassword("123456");
        /*连接池*/
        basicDataSource.setInitialSize(10);
        basicDataSource.setMaxTotal(10);
        basicDataSource.setMaxIdle(5);
        basicDataSource.setMinIdle(5);
        return basicDataSource;
    }

    @Bean
    public BasicDataSource basicDataSource2() {
        BasicDataSource basicDataSource = new BasicDataSource();
        /*数据库链接的基本配置信息*/
        basicDataSource.setDriverClassName(driverClassName);
        basicDataSource.setUrl(url);
        basicDataSource.setUsername("root");
        basicDataSource.setPassword("123456");
        /*连接池*/
        basicDataSource.setInitialSize(10);
        basicDataSource.setMaxTotal(10);
        basicDataSource.setMaxIdle(5);
        basicDataSource.setMinIdle(5);
        return basicDataSource;
    }

    @Bean  //参数=》自动注入==》根据类型注入==>根据方法名称id注入=》指定
    public SqlSessionFactoryBean sqlSessionFactoryBean3(@Qualifier("basicDataSource1") BasicDataSource dataSource) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
//        sqlSessionFactoryBean.setTypeAliasesPackage("com.hyl.pojo");
//        sqlSessionFactoryBean.setMapperLocations(new org.springframework.core.io.ClassPathResource("mapper/UserMapper.xml"));
        return sqlSessionFactoryBean;
    }

    @Bean  //参数=》自动注入==》根据类型注入==>根据方法名称id注入
    public SqlSessionFactoryBean sqlSessionFactoryBean2(BasicDataSource basicDataSource2) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(basicDataSource2);
        return sqlSessionFactoryBean;
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean1() {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(basicDataSource1());
        return sqlSessionFactoryBean;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.hyl.mapper");
        return mapperScannerConfigurer;
    }
}
